/*
	GeorgeLib
	Camera.cpp
	Under MIT License
	*/

#include "Camera.h"

namespace GeorgeLib
{
	Camera::Camera(void)
	{
		Reset();
	}

	Camera::Camera(vec3 Pos)
		: pos(Pos)
	{
		Reset();
	}

	Camera::Camera(vec3 Pos, vec3 Dir)
		: pos(Pos)
		, dir(Dir)
	{
		Reset();
	}

	Camera::~Camera(void)
	{
	}

	void Camera::Update(void)
	{
		gluLookAt
			(
			pos.x, pos.y, pos.z,							// Position of camera
			pos.x + dir.x, pos.y + dir.y, pos.z + dir.z,	// Direction of camera
			up.x, up.y, up.z								// Orientation of camera
			);
	}

	void Camera::Reset(void)
	{
		pos(1, 1, 1);
		dir(-1, -1, -1);
		right(1, 0, 0);
		up(0, 1, 0);
		max_speed = 20;
	}

	void Camera::Pitch(float theta)
	{
		dir = dir.rotateabout(theta, dir ^ up);
	}

	void Camera::Yaw(float theta)
	{
		dir = dir.rotateabout(theta, up);
	}

	void Camera::Roll(float theta)
	{
		up = up.rotateabout(theta, dir);
	}

	void Camera::Walk(float delta)
	{
		// limit the speed
		if (delta != 0 && delta > max_speed)
			delta = max_speed;

		vec3 temp = dir * (delta / 10);
		pos(temp.x, pos.y, temp.z);
	}

	void Camera::Strafe(float delta)
	{
		// limit the speed
		if (delta != 0 && delta > max_speed)
			delta = max_speed;

		// assign right vector as the cross product of the up and direction vectors
		right = (dir ^ up).normal();

		pos += right * (delta / 10);
	}

	void Camera::Fly(float delta)
	{
		// limit the speed
		if (delta != 0 && delta > max_speed)
			delta = max_speed;

		pos += dir * (delta / 10);
	}
} // namespace GeorgeLib